package com.jsy.common.util.publicApi.aksk;


//import com.cwn.wizbank.services.ApiKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * API接口示例和密钥生成
 */
@RestController
@RequestMapping("/api")
public class AkSkController {

//    @Autowired
//    private ApiKeyService apiKeyService;

    @RequestMapping("/getExample")
    public String getData(@RequestHeader Map<String, String> headers,
                          @RequestParam Map<String, String> queryParams) {
        if (!AkSkAuthenticator.verifyRequest("GET", "/app/api/getExample", queryParams, null, headers)) {
            throw new RuntimeException("Invalid signature");
        }
        return "Sensitive data response";
    }

    @RequestMapping(value = "/postExample",method = RequestMethod.POST)
    public String submitData(@RequestHeader Map<String, String> headers,
                             @RequestParam Map<String, String> queryParams,
                             @RequestBody(required = false) String requestBody) {
        if (!AkSkAuthenticator.verifyRequest("POST", "/app/api/postExample", queryParams, requestBody, headers)) {
            throw new RuntimeException("Invalid signature");
        }
        return "Data processed successfully";
    }

    @RequestMapping(value = "/generate-key",method = RequestMethod.POST)
    public Map<String, String> generateKeyPair(HttpServletRequest request) {
        // 注意：此接口需要额外权限控制，不能公开访问
        // 生成AK/SK对
//        return apiKeyService.createApiKey();
        return null;
    }

}